package com.peanutplan.blog.controller.admin;

import com.github.pagehelper.PageInfo;
import com.peanutplan.blog.controller.BaseController;
import com.peanutplan.blog.dao.UserVoMapper;
import com.peanutplan.blog.exception.TipException;
import com.peanutplan.blog.model.bo.RestResponseBo;
import com.peanutplan.blog.model.vo.CommentVo;
import com.peanutplan.blog.model.vo.CommentVoExample;
import com.peanutplan.blog.model.vo.UserVo;
import com.peanutplan.blog.model.vo.UserVoExample;
import com.peanutplan.blog.service.ICommentService;
import com.peanutplan.blog.service.IUserService;
import com.peanutplan.blog.utils.CommonUtil;
import com.vdurmont.emoji.EmojiParser;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by 13 on 2017/2/26.
 */
@Controller
@RequestMapping("admin/user")
public class UserController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Resource
    private ICommentService commentsService;
    
    @Resource
    private IUserService userService;
    
    @Resource
    private UserVoMapper userDao;

    /**
     * 用户列表
     * @param page
     * @param limit
     * @param request
     * @return
     */
    @GetMapping(value = "")
    public String index(@RequestParam(value = "page", defaultValue = "1") int page,
                        @RequestParam(value = "limit", defaultValue = "15") int limit, HttpServletRequest request) {
        UserVoExample userVoExample = new UserVoExample();
        userVoExample.setOrderByClause("created desc");
        PageInfo<UserVo> commentsPaginator = userService.getUsersWithPage(userVoExample, page, limit);
        request.setAttribute("users", commentsPaginator);
        
        return "admin/users_list";
    }

    /**
     * 删除一条评论
     * @param coid
     * @return
     */
    @PostMapping(value = "delete")
    @ResponseBody
    @Transactional(rollbackFor = TipException.class)
    public RestResponseBo delete(@RequestParam Integer uid) {
        try {
            UserVo user = userService.getUserById(uid);
            if("admin".equals(user.getUsername())) {
            	return RestResponseBo.fail("不能删除管理员");
            }
            if(null == user){
                return RestResponseBo.fail("不存在该用户");
            }
            userService.delete(user.getUid());
        } catch (Exception e) {
            String msg = "用户删除失败";
            if (e instanceof TipException) {
                msg = e.getMessage();
            } else {
                LOGGER.error(msg, e);
            }
            return RestResponseBo.fail(msg);
        }
        return RestResponseBo.ok();
    }
    
    @PostMapping(value = "regist")
    @ResponseBody
    public RestResponseBo delete(String username, String password, String email) {
        try {
            UserVoExample filter1 = new UserVoExample();
            filter1.createCriteria().andUsernameEqualTo(username);
            if(userDao.countByExample(filter1)>0) {
            	return RestResponseBo.fail("用户名:"+username+" 重复，请重新取一个昵称！");
            }
            
            UserVoExample filter2 = new UserVoExample();
            filter2.createCriteria().andEmailEqualTo(email);
            if(userDao.countByExample(filter2)>0) {
            	return RestResponseBo.fail("邮箱:"+email+" 重复，请重新输入邮箱！");
            }
            
            UserVo u = new UserVo();
            u.setUsername(username);
            u.setPassword(CommonUtil.MD5encode(username+password));
            u.setEmail(email);
            userDao.insert(u);
        } catch (Exception e) {
            String msg = "用户注册失败";
            if (e instanceof TipException) {
                msg = e.getMessage();
            } else {
                LOGGER.error(msg, e);
            }
            return RestResponseBo.fail(msg);
        }
        return RestResponseBo.ok();
    }

}
